const host = "https://test.the3dspace.cn"
module.exports = {
	checkMemberToken: async (token) => {
		const res = await uniCloud.httpclient.request(host + "/api-uaa/oauth/member/checkToken", {
		    method: 'GET',
		    data: {token: token},
			dataType: "json"
		})
		return res.data;
	},
	getMemberByToken: async (token) => {
		const res = await uniCloud.httpclient.request(host + "/api-uaa/oauth/member/getMemberByToken", {
		    method: 'GET',
		    data: {token},
			dataType: "json"
		})
		return res.data;
	},
	getAllAdminUnionId: async (token) => {
		const res = await uniCloud.httpclient.request(host + "/api-user/sys/sysUser/getAllAdminWxUnionIds", {
		    method: 'GET',
		    headers: {
		    	"Authorization": "bearer " + token
		    },
			dataType: "json"
		})
		return res.data;
	},
	checkUserToken: async (token) => {
		const res = await uniCloud.httpclient.request(host + "/api-uaa/oauth/user/checkToken", {
		    method: 'GET',
		    data: {token},
			dataType: "json"
		})
		return res.data;
	},
	getUserByToken: async (token) => {
		const res = await uniCloud.httpclient.request(host + "/api-uaa/oauth/user/getUserByToken", {
		    method: 'GET',
		    data: {token},
			dataType: "json"
		})
		return res.data;
	},
	checkPwd: async (pwd1, pwd2) => {
		const res = await uniCloud.httpclient.request(host + "/api-uaa/oauth/user/checkPwd", {
		    method: 'GET',
		    data: {
				pwd1,
				pwd2
			},
			dataType: "json"
		})
		return res.data;
	},
	getUserById: async (token, id) => {
		const res = await uniCloud.httpclient.request(host + "/api-user/users/" + id, {
		    method: 'GET',
			dataType: "json",
			headers: {
				"Authorization": "bearer " + token
			}
		})
		
		return res.data;
	},
	getStoreById: async (id) => {
		const res = await uniCloud.httpclient.request(host + "/api-user/sys/sysStore/notAuth/getStoreListByIds", {
		    method: 'GET',
			dataType: "json",
			data: {
				storeIds: id
			}
		})
		if (res.data.data && res.data.data.length === 1) {
			return res.data.data[0]
		}
	},
	consumeService: async (token, memberId, storeId) => {
		let res;
		try{
			res = await uniCloud.httpclient.request(host + "/api-trade/vipchargeoff/consumeService" + id, {
			    method: 'GET',
				dataType: "json",
				headers: {
					"Authorization": "bearer " + token
				}
			})
		}catch(e){
			throw e
		}
		if (res.data.code !== 200) {
			throw new Error(res.data.msg)
		}
	},
	memberEnter: async (token, memberId) => {
		let res;
		try{
			res = await uniCloud.httpclient.request(host + "/api-trade/vipchargeoff/memberEnter" + id, {
			    method: 'GET',
				dataType: "json",
				headers: {
					"Authorization": "bearer " + token
				}
			})
		}catch(e){
			throw e
		}
		if (res.data.code !== 200) {
			throw new Error(res.data.msg)
		}
	},
	memberOut: async (token, {memberId, employeeId, bossId, price}) => {
		let res;
		try{
			res = await uniCloud.httpclient.request(host + "/api-trade/vipchargeoff/memberOut" + id, {
			    method: 'POST',
				dataType: "json",
				data: {
					memberId, 
					employeeId, 
					bossId, 
					price
				},
				headers: {
					"Authorization": "bearer " + token
				}
			})
		}catch(e){
			throw e
		}
		if (res.data.code !== 200) {
			throw new Error(res.data.msg)
		}
	},
	saveUser: async (token, user) => {
		const res = await uniCloud.httpclient.request(host + "/api-user/sys/sysUser/register", {
		    method: 'POST',
			data: user,
			dataType: "json",
			headers: {
				"Authorization": "bearer " + token
			}
		})
	},
	delUser: async (token, userId) => {
		const res = await uniCloud.httpclient.request(host + "/api-user/sys/sysUser/delete/" + userId, {
		    method: 'GET',
			dataType: "json",
			headers: {
				"Authorization": "bearer " + token
			}
		})
	},
	getMemberByIds: async (ids) => {
		const res = await uniCloud.httpclient.request(host + "/api-member/notAuth/getMembersByIds", {
		    method: 'POST',
			dataType: "json",
			data: ids,
			contentType: "json"
		})
		return res.data.data
	},
	isAdmin: (user) => {
		let admin = user.roles.find((item) => {
			return item.code === "admin"
		});
		if (admin) {
			return true;
		}
		return false;
	},
	isStoreAdmin2: (user) => {
		let admin = user.roles.find((item) => {
			return item.code === "store_admin_2"
		});
		if (admin) {
			return true;
		}
		return false;
	},
	isStoreEmployee2: (user) => {
		let admin = user.roles.find((item) => {
			return item.code === "store_employee_2"
		});
		if (admin) {
			return true;
		}
		return false;
	}
}
